define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'lal/member/index' + location.search,
                    add_url: 'lal/member/add',
                    edit_url: 'lal/member/edit',
                    del_url: 'lal/member/del',
                    multi_url: 'lal/member/multi',
                    import_url: 'lal/member/import',
                    detail_url: 'lal/member/detail',
                    syncbankaccount_url: 'lal/member/syncbankaccount',
                    getbankcardurl_url: 'lal/member/getbankcardurl',
                    table: 'lal_member',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                Search:true,
                commonSearch:true,
                searchFormVisible:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'type', title: __('Type'), searchList: {"0":__('Type 0'),"1":__('Type 1')}, formatter: Table.api.formatter.normal},
                       // {field: 'shop_id', title: __('Shop_id')},
                       {field: 'member_no', title: __('会员编号'), operate: false},
                        {field: 'shopname', title: __('Shopname'), operate: 'LIKE'},
                        {field: 'url', title: __('Url'), operate: 'LIKE', formatter: Table.api.formatter.url, operate: false},
                        {field: 'bank_card_url', title: __('银行卡变更'), operate: 'LIKE', formatter: Table.api.formatter.url, operate: false},
                        {field: 'username', title: __('Username'), operate: false},
                       // {field: 'lal_id', title: __('Lal_id')},
                        {field: 'abalance', title: __('Abalance'), operate: false},
                        {field: 'bbalance', title: __('Bbalance'), operate: false},
                        {field: 'bank', title: __('Bank'), operate: false},
                        {field: 'name', title: __('Name'), operate: false},
                        {field: 'card', title: __('Card'), operate: false},
                       // {field: 'remarks', title: __('Remarks'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime, operate: false},
                       // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons :[
                                {
                                    name: 'detail',
                                    text: '详情',
                                    title: '查看店铺详情',
                                    icon: '',
                                    extend: 'data-area=\'["50%", "50%"]\'',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: 'lal/member/detail'
                                },
                                {
                                    name:'syncbankaccount',
                                    text:'同步信息',
                                    title:'同步银行卡信息',
                                    classname: 'btn btn-xs btn-info btn-view btn-ajax',
                                    icon: 'fa',
                                    url: 'lal/member/syncbankaccount',
                                    refresh:true
                                },
                                {
                                    name:'getbankcardurl',
                                    text:'更改银行卡',
                                    title:'更改银行卡',
                                    classname: 'btn btn-xs btn-info btn-view btn-ajax',
                                    icon: 'fa',
                                    url: 'lal/member/getbankcardurl',
                                    refresh:true
                                }
                            ],
                            callback: function (data) {
                                table.bootstrapTable('refresh', {});
                                return false;
                            },
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'lal/member/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'lal/member/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'lal/member/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            $(document).on("change", "input[name='row[type]']", function () {
                var type = $('input[name="row[type]"]:checked').prop('value');
                if (type == 0) {
                    $('#c-shop_id_text').selectPageClear();
                    $('#c-shop_id_text').attr('placeholder','选择门店');
                    $("#c-shop_id_text").data("selectPageObject").option.data = "lal/member/getshoptypeList?type=1";
                }else{
                    $('#c-shop_id_text').selectPageClear();
                    $('#c-shop_id_text').attr('placeholder','选择服务商');
                    $("#c-shop_id_text").data("selectPageObject").option.data = "lal/member/getshoptypeList?type=2";
                }
            });

            Controller.api.bindevent();   
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            $(document).on("click", ".btn-embossed", function () {
                Fast.api.close();
            })
            Controller.api.bindevent();
        },
        syncbankaccount: function () {
            Controller.api.bindevent();
        },
        getbankcardurl: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
